'use strict'

var escapeHTML = require('../Utils').escapeHTML

var domQuery = require('min-dom').query

var entryFieldDescription = require('./EntryFieldDescription')

var textField = function(options, defaultParameters) {
  // Default action for the button next to the input-field
  var defaultButtonAction = function(element, inputNode) {
    var input = domQuery('input[name="' + options.modelProperty + '"]', inputNode)
    input.value = ''

    return true
  }

  // default method to determine if the button should be visible
  var defaultButtonShow = function(element, inputNode) {
    var input = domQuery('input[name="' + options.modelProperty + '"]', inputNode)

    return input.value !== ''
  }

  var resource = defaultParameters
  var label = options.label || resource.id
  var dataValueLabel = options.dataValueLabel
  var buttonLabel = (options.buttonLabel || 'X')
  var actionName = (typeof options.buttonAction !== 'undefined') ? options.buttonAction.name : 'clear'
  var actionMethod = (typeof options.buttonAction !== 'undefined') ? options.buttonAction.method : defaultButtonAction
  var showName = (typeof options.buttonShow !== 'undefined') ? options.buttonShow.name : 'canClear'
  var showMethod = (typeof options.buttonShow !== 'undefined') ? options.buttonShow.method : defaultButtonShow
  var canBeDisabled = !!options.disabled && typeof options.disabled === 'function'
  var canBeHidden = !!options.hidden && typeof options.hidden === 'function'
  var description = options.description
  // resource.html =
  //   '<label for="camunda-' + escapeHTML(resource.id) + '" ' +
  //     (canBeDisabled ? 'data-disable="isDisabled" ' : '') +
  //     (canBeHidden ? 'data-show="isHidden" ' : '') +
  //     (dataValueLabel ? 'data-value="' + escapeHTML(dataValueLabel) + '"' : '') + '>' + escapeHTML(label) + '</label>' +
  //   '<div class="bpp-field-wrapper" ' +
  //     (canBeDisabled ? 'data-disable="isDisabled"' : '') +
  //     (canBeHidden ? 'data-show="isHidden"' : '') +
  //     '>' +
  //     '<input id="camunda-' + escapeHTML(resource.id) + '" type="text" name="' + escapeHTML(options.modelProperty) + '" ' +
  //       (canBeDisabled ? 'data-disable="isDisabled"' : '') +
  //       (canBeHidden ? 'data-show="isHidden"' : '') +
  //       ' />' +
  //     '<button class="' + escapeHTML(actionName) + '" data-action="' + escapeHTML(actionName) + '" data-show="' + escapeHTML(showName) + '" ' +
  //       (canBeDisabled ? 'data-disable="isDisabled"' : '') +
  //       (canBeHidden ? ' data-show="isHidden"' : '') + '>' +
  //       '<span>' + escapeHTML(buttonLabel) + '</span>' +
  //     '</button>' +
  //   '</div>'
  var arry = ['assignee', 'candidateUsers', 'candidateGroups', 'candidateStarterGroups', 'candidateStarterUsers', 'initiator', 'multiInstance-collection']
  if (arry.indexOf(resource.id) >= 0) {
    let button
    if (resource.id === 'assignee') {
      button = '<input type="button" class="btn-select user-role-select" value="选择" id="' + resource.id + '"/>' +
      '<input type="button" class="btn-select user-role-select-more" value="更多" id="' + resource.id + '-more"/>'// 点击方法
    } else {
      button = '<input type="button" class="btn-select user-role-select" value="选择" id="' + resource.id + '"/>' // 点击方法
    }
    resource.html =
      '<label for="camunda-' + resource.id + '" ' +
      (canBeDisabled ? 'data-disable="isDisabled" ' : '') +
      (canBeHidden ? 'data-show="isHidden" ' : '') +
      (dataValueLabel ? 'data-value="' + dataValueLabel + '"' : '') + '>' + label + '</label>' +
      '<div class="bpp-field-wrapper" ' +
      (canBeDisabled ? 'data-disable="isDisabled"' : '') +
      (canBeHidden ? 'data-show="isHidden"' : '') +
      '>' +
      '<div class="left-input-disabled">' +
      '<input id="camunda-' + resource.id + '" type="text" name="' + options.modelProperty + '" ' +
      (canBeDisabled ? 'data-disable="isDisabled"' : '') +
      (canBeHidden ? 'data-show="isHidden"' : '') +
      ' />' +

      '<button class="' + actionName + '" data-action="' + actionName + '" data-show="' + showName + '" ' +
      (canBeDisabled ? 'data-disable="isDisabled"' : '') +
      (canBeHidden ? ' data-show="isHidden"' : '') + '>' +
      '<span>' + buttonLabel + '</span>' +
      '</button>' +
      '</div>' +
      button + // 点击方法
      '</div>'
  } else {
    resource.html =
    '<label for="camunda-' + escapeHTML(resource.id) + '" ' +
      (canBeDisabled ? 'data-disable="isDisabled" ' : '') +
      (canBeHidden ? 'data-show="isHidden" ' : '') +
      (dataValueLabel ? 'data-value="' + escapeHTML(dataValueLabel) + '"' : '') + '>' + escapeHTML(label) + '</label>' +
    '<div class="bpp-field-wrapper" ' +
      (canBeDisabled ? 'data-disable="isDisabled"' : '') +
      (canBeHidden ? 'data-show="isHidden"' : '') +
      '>' +
      '<input id="camunda-' + escapeHTML(resource.id) + '" type="text" name="' + escapeHTML(options.modelProperty) + '" ' +
        (canBeDisabled ? 'data-disable="isDisabled"' : '') +
        (canBeHidden ? 'data-show="isHidden"' : '') +
        ' />' +
      '<button class="' + escapeHTML(actionName) + '" data-action="' + escapeHTML(actionName) + '" data-show="' + escapeHTML(showName) + '" ' +
        (canBeDisabled ? 'data-disable="isDisabled"' : '') +
        (canBeHidden ? ' data-show="isHidden"' : '') + '>' +
        '<span>' + escapeHTML(buttonLabel) + '</span>' +
      '</button>' +
    '</div>'
  }

  // add description below text input entry field
  if (description) {
    resource.html += entryFieldDescription(description)
  }

  resource[actionName] = actionMethod
  resource[showName] = showMethod

  if (canBeDisabled) {
    resource.isDisabled = function() {
      return options.disabled.apply(resource, arguments)
    }
  }

  if (canBeHidden) {
    resource.isHidden = function() {
      return !options.hidden.apply(resource, arguments)
    }
  }

  resource.cssClasses = ['bpp-textfield']

  return resource
}

module.exports = textField
